//
//  RankController.m
//  Kugou-OC
//
//  Created by BH on 16/5/27.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "RankController.h"
#import "NetworkHelper.h"
#import "RankCell.h"
#import "RankModel.h"
#import "GlobalDefine.h"
#import "RankFirstLevelController.h"
#import "RankSecondLevelController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface RankController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *rankTable;
@property(nonatomic,strong)NSMutableArray *rankArray;

@end

@implementation RankController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _rankTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-90-64-30) style:UITableViewStylePlain];
    _rankTable.dataSource = self;
    _rankTable.delegate = self;
    [self.view addSubview:_rankTable];
    
    [self getData];
}

-(void)getData{
    _rankArray = [NSMutableArray array];
    
    NSString *urlStr = @"http://ioscdn.kugou.com/api/v3/rank/list?plat=2&withsong=1&showtype=2&parentid=0&apiver=3&version=8100";
    
    [[NetworkHelper shareNetwork] getDataWithUrl:urlStr view:self.view success:^(id response) {
        
        NSArray *infoArr = response[@"data"][@"info"];
        for (NSDictionary *dict in infoArr) {
            RankModel *model = [[RankModel alloc] initWithDict:dict];
            [_rankArray addObject:model];
        }
        
        [_rankTable reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

#pragma mark - UITableview datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rankArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RankCell *cell = [RankCell cellWithTableView:tableView];
    
    cell.indexPath = indexPath;
    RankModel *model = _rankArray[indexPath.row];
    cell.rankM = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RankModel *model = _rankArray[indexPath.row];
    
    if (indexPath.row == 0) {
        RankFirstLevelController *first = ALLOC(RankFirstLevelController);
        first.rankModel = model;
        PUSH(first);
    }else{
        RankSecondLevelController *second = ALLOC(RankSecondLevelController);
        second.rankModel = model;
        PUSH(second);
    }
}

@end
